package com.topsmob.processer;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by lyh on 2017/6/7.
 */
public class PcOnlieProcesser implements PageProcessor {
    public static final String savePath = "C:\\pc4\\";
    //public static final String savePath = "/home/wwwroot/a.cuxiao100.cn/pconline4/";
    public String firstTitle = null;
    public int   i = 1;//第一个
    public void process(Page page) {
        page.putField("title",page.getHtml().xpath("//*[@id=\"J-images\"]/h3/text()").toString());
        String title = page.getHtml().xpath("//*[@id=\"J-images\"]/h3/text()").toString();

        page.putField("img",page.getHtml().xpath("//*[@id=\"imgBox\"]/a/img/@src").toString());
        String imageUrl = page.getHtml().xpath("//*[@id=\"imgBox\"]/a/img/@src").toString();
        page.putField("desc",page.getHtml().xpath("//div[@class=\"cor-des\"]/text()").toString());
        String desc = page.getHtml().xpath("//div[@class=\"cor-des\"]/text()").toString();
        String pageUrl = page.getHtml().xpath("//div[@id=\"J-image\"]/a[@class=\"abtn abtn-r\"]/@href").toString().replace("../","http://pp.pcbaby.com.cn/");
        if(pageUrl==null || (firstTitle != null && firstTitle .equals(title) && i==1)){
            System.out.println("break;;;;;;");
            page.setSkip(true);
        }else {
            if(firstTitle == null){
                firstTitle = title;

            }
            i++;
            if(!firstTitle.equals(title) ){
                i = 1;
            }

            System.out.println(pageUrl);
            page.addTargetRequest(new Request(pageUrl));

            String imgindex = page.getHtml().xpath("//p[@class=\"col-pic-count\"]/strong/text()").toString();
            System.out.println(imgindex);
            try {
                wireteFile(imgindex+":"+desc,title+".txt",savePath+title);
                download("http:"+imageUrl,imgindex+".jpg",savePath+title);
            } catch (Exception e) {
                System.out.println(e.getLocalizedMessage());
            }
        }



    }

    public Site getSite() {

        return Site.me().setRetryTimes(2).setTimeOut(30*1000).setDomain("http://pp.pcbaby.com.cn/").setCharset("gb2312");
    }

    public static void main(String[] args) {
        //最后一次  29588
        Spider.create(new PcOnlieProcesser()).addUrl("http://pp.pcbaby.com.cn/photo/27708.html").thread(5)
                .run();
    }

    public void  wireteFile(String content,String fileName,String savePath ) throws Exception{
        //FileWriter fileWriter = null;


        File dirfile = new File(savePath);
        if(!dirfile.exists()){
            dirfile.mkdirs();
        }
        File file = new File(savePath+File.separator+fileName);
        if(!file.exists()){
            file.createNewFile();
        }
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file,true),"UTF-8");
        PrintWriter printWriter = new PrintWriter(out);
        printWriter.println(content);
        printWriter.flush();
        out.flush();
        printWriter.close();
        out.close();

    }

    public  void download(String urlString, String filename,String savePath) throws Exception {
        // 构造URL
        URL url = new URL(urlString);
        // 打开连接
        URLConnection con = url.openConnection();
        //设置请求超时为5s
        con.setConnectTimeout(30*1000);
        // 输入流
        InputStream is = con.getInputStream();

        // 1K的数据缓冲
        byte[] bs = new byte[1024];
        // 读取到的数据长度
        int len;
        // 输出的文件流
        File sf=new File(savePath);
        if(!sf.exists()){
            sf.mkdirs();
        }
        OutputStream os = new FileOutputStream(sf.getPath()+File.separator+filename);
        // 开始读取
        while ((len = is.read(bs)) != -1) {
            os.write(bs, 0, len);
        }
        // 完毕，关闭所有链接
        os.close();
        is.close();
    }
}
